package com.zhoug.common3.share.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.zhoug.common3.R;
import com.zhoug.common3.share.ClientType;
import com.zhoug.common3.share.ShareCallback;
import com.zhoug.common3.share.ShareClient;
import com.zhoug.common3.share.ShareClientImp;
import com.zhoug.common3.share.ShareParams;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


/**
 * 分享控件
 *
 * @Author 35574
 * @Date 2021/5/21
 * @Description
 */
public class ShareView extends FrameLayout implements ShareAdapter.OnItemClickListener, ShareCallback {
    private static final String TAG = ">>>ShareView";
    private RecyclerView recyclerView;
    private int spanCount = 4;
    private ShareParams shareParams;
    private ShareClient shareClient;

    public static final int CLIENT_Wechat = 1;//000001
    public static final int CLIENT_WechatMoments = 2;//000010
    public static final int CLIENT_QQ = 4;//000100
    public static final int CLIENT_QZone = 8;//001000
    public static final int CLIENT_SinaWeibo = 16;//010000
    public static final int CLIENT_Local = 32;//100000
    public static final int CLIENT_ALL = 63;//111111
    private int clientFlag = CLIENT_ALL;


    public ShareView(@NonNull Context context) {
        this(context, null);
    }

    public ShareView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ShareView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View view = LayoutInflater.from(context).inflate(R.layout.share_view, this, false);
        recyclerView = view.findViewById(R.id.recyclerView_share);
        addView(view);

    }

    public void init() {
        shareClient = new ShareClientImp(getContext());
        shareClient.setShareCallback(this);

        recyclerView.setLayoutManager(new GridLayoutManager(getContext(), spanCount));
        ShareAdapter shareAdapter = new ShareAdapter(getContext());
        shareAdapter.setData(initShareBeans());
        shareAdapter.setOnItemClickListener(this);
        recyclerView.setAdapter(shareAdapter);

    }


    private List<ShareBean> initShareBeans() {
        List<ShareBean> shareBeans = new ArrayList<>();
        if ((clientFlag & CLIENT_Wechat) == CLIENT_Wechat) {
            shareBeans.add(new ShareBean("微信", R.drawable.share_icon_wechat, ClientType.Wechat));
        }
        if ((clientFlag & CLIENT_WechatMoments) == CLIENT_WechatMoments) {
            shareBeans.add(new ShareBean("朋友圈", R.drawable.share_icon_wechat_moment, ClientType.WechatMoments));
        }
        if ((clientFlag & CLIENT_QQ) == CLIENT_QQ) {
            shareBeans.add(new ShareBean("QQ", R.drawable.share_icon_qq, ClientType.QQ));
        }
        if ((clientFlag & CLIENT_QZone) == CLIENT_QZone) {
            shareBeans.add(new ShareBean("QQ空间", R.drawable.share_icon_qqzone, ClientType.QZone));
        }
        if ((clientFlag & CLIENT_SinaWeibo) == CLIENT_SinaWeibo) {
            shareBeans.add(new ShareBean("新浪微博", R.drawable.share_icon_sinaweibo, ClientType.SinaWeibo));
        }
        if ((clientFlag & CLIENT_Local) == CLIENT_Local) {
            shareBeans.add(new ShareBean("保存本地", R.drawable.share_icon_local, ClientType.Local));
        }
        return shareBeans;
    }


    @Override
    public void onItemClick(ShareAdapter adapter, ShareBean item, int position) {
        if (callback != null) {
            boolean b = callback.onShareImageClick(item.getClientType());
            if (!b) {
                shareClient.share(getContext(), item.getClientType(), shareParams);
            }
        } else {
            shareClient.share(getContext(), item.getClientType(), shareParams);
        }
    }

    @Override
    public void onKeepLocalSuccess(String keepPath) {
        toast(keepPath);
    }

    @Override
    public void onShareError(Throwable error) {
        if (error != null) {
            toast(error.getMessage());
        }
    }

    private void toast(String msg) {
        Toast.makeText(getContext(), msg != null ? msg : "", Toast.LENGTH_SHORT).show();
    }


    public ShareParams getShareParams() {
        return shareParams;
    }

    public ShareView setShareParams(ShareParams shareParams) {
        this.shareParams = shareParams;
        return this;
    }

    public ShareView setSpanCount(int spanCount) {
        this.spanCount = spanCount;
        return this;
    }

    /**
     * 设置分享的平台,会清除以前的平台设置
     *
     * @param clientFlag
     * @return
     */
    public ShareView setClientFlag(int clientFlag) {
        this.clientFlag = clientFlag;
        return this;
    }

    /**
     * 清除平台设置
     *
     * @return
     */
    public ShareView clearClientFlag() {
        this.clientFlag = 0;
        return this;
    }

    /**
     * 增加分享的平台
     *
     * @param clientFlag
     * @return
     */
    public ShareView addClientFlag(int clientFlag) {
        this.clientFlag |= clientFlag;
        return this;
    }

    private Callback callback;

    public void setCallback(Callback callback) {
        this.callback = callback;
    }


    public interface Callback {
        /**
         * @param clientType {@link ClientType}
         * @return true:消耗了事件,不会调用对应的分享方法,false:未消耗事件,自动调用对应的分享方法,
         */
        boolean onShareImageClick(ClientType clientType);
    }
}
